﻿
using Microsoft.Toolkit.Mvvm.ComponentModel;
using Microsoft.Toolkit.Mvvm.Messaging;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using Microsoft.Toolkit.Mvvm.Input;
using Microsoft.Win32;
using Microsoft.WindowsAPICodePack.Dialogs;

namespace WinRenamer.ViewModels
{
    public class MainViewModel : ObservableObject
    {
        public MainViewModel()
        {
            this.Folder = "请选择文件夹";
            this.TxtFile = "请选择txt文件";
        }

        private string folder;
        public string Folder
        {
            get => folder;
            set => SetProperty(ref folder, value);
        }

        private string txtFile;
        public string TxtFile
        {
            get => txtFile;
            set => SetProperty(ref txtFile, value);
        }



        private string message;
        public string Message
        {
            get => message;
            set => SetProperty(ref message, value);
        }


        private bool isBrowseFoldering;
        private RelayCommand browseFolderCommand;

        public RelayCommand BrowseFolderCommand
        {
            get
            {
                return browseFolderCommand
                  ?? (browseFolderCommand = new RelayCommand(
                    async () =>
                    {
                        if (isBrowseFoldering)
                        {
                            return;
                        }

                        isBrowseFoldering = true;
                        BrowseFolderCommand.NotifyCanExecuteChanged();

                        BrowseFolder();

                        isBrowseFoldering = false;
                        BrowseFolderCommand.NotifyCanExecuteChanged();
                    },
                    () => !isBrowseFoldering));
            }
        }

        public void BrowseFolder()
        {
            var dialog = new CommonOpenFileDialog();
            dialog.IsFolderPicker = true;
            dialog.Multiselect = false;
            if (dialog.ShowDialog() == CommonFileDialogResult.Ok)
            {
                this.Folder = dialog.FileName;
                StartCommand.NotifyCanExecuteChanged();
            }
        }

        private bool isBrowseTxting;
        private RelayCommand browseTxtCommand;

        public RelayCommand BrowseTxtCommand
        {
            get
            {
                return browseTxtCommand
                  ?? (browseTxtCommand = new RelayCommand(
                    async () =>
                    {
                        if (isBrowseTxting)
                        {
                            return;
                        }

                        isBrowseTxting = true;
                        BrowseTxtCommand.NotifyCanExecuteChanged();

                        BrowseTxt();

                        isBrowseTxting = false;
                        BrowseTxtCommand.NotifyCanExecuteChanged();
                    },
                    () => !isBrowseTxting));
            }
        }


        public bool IsInputValid
        {
            get
            {
                return Directory.Exists(this.Folder) && File.Exists(this.TxtFile);
            }
        }

        public void BrowseTxt()
        {
            var openFileDialog = new OpenFileDialog()
            {
                Filter = "Text files (*.txt)|*.txt|All files (*.*)|*.*",
                Multiselect = false
            };

            if (openFileDialog.ShowDialog() == true)
            {
                this.TxtFile = openFileDialog.FileName;
                StartCommand.NotifyCanExecuteChanged();
            }
        }

        private bool isStarting;
        private RelayCommand startCommand;

        public RelayCommand StartCommand
        {
            get
            {
                return startCommand
                  ?? (startCommand = new RelayCommand(
                    async () =>
                    {
                        if (isStarting)
                        {
                            return;
                        }

                        isStarting = true;
                        StartCommand.NotifyCanExecuteChanged();

                        Start();

                        isStarting = false;
                        StartCommand.NotifyCanExecuteChanged();
                    },
                    () => !isStarting && IsInputValid));
            }
        }

        public void Start()
        {
            foreach (string line in File.ReadAllLines(this.TxtFile, System.Text.Encoding.UTF8))
            {
                Environment.CurrentDirectory = this.Folder;

                string[] parts = line.Split(new char[] { ' ', '\t' }, StringSplitOptions.RemoveEmptyEntries);
                if (parts.Length == 2)
                {
                    string from = parts[0].Trim();
                    string to = parts[1].Trim();
                    this.Message += $"{from}--->{to}" + Environment.NewLine;
                    try
                    {
                        if (Directory.Exists(from))
                            Directory.Move(from, to);
                        else if (File.Exists(from))
                            File.Move(from, to);
                    }
                    catch (Exception ex)
                    {
                        this.Message += ex.Message + Environment.NewLine;
                    }

                }
            }
            this.Message += "重命名结束!";

        }


    }
}
